package com.colin.aiyi.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.presenter.Interface.IPresenter;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.utils.SharePreferenceUtil;
import com.colin.aiyi.view.ShowMsgUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by hl on 2017/5/4.
 */

public class BaseActivity extends AppCompatActivity implements IViewAc,View.OnClickListener{
    public IPresenter iPresenter;
    public User user;
    private long exitTime;
    private ProgressDialog dialog;


    /*个人建议在onResume注册EventBus
    *在可见可交互状态下注册，尽可能少的占用内存
    */
    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }
    /*个人建议在onPause注册EventBus(将当前Activity注册为事件订阅者)
     *不影响功能的情况下提早解除注册，尽可能少的占用内存
     */
    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
     public <T extends BaseResponse>void  checkResponseBase(T t){
        ShowMsgUtil.hideLoadingDialog();
        hideLoading();
        if (t instanceof BaseResponse)
        {
            if (t==null)
                return;
           switch (t.getCode()){
               case 200:
                   break;
               case  400:
                 if (t.getMsgType()== Constant.CODE_AUDIO_detail){
                    return;
                 }
                   showMsg(t.getMessage());
                   break;
               case 4005:
                   if((System.currentTimeMillis()-exitTime) > 2000){
                       exitTime = System.currentTimeMillis();
                       SharePreferenceUtil.removeObject(this,User.class);
                       SharePreferenceUtil.saveObject(this,new User());
                       Intent intent = new Intent(getContext(), LoginAc.class);
                       intent.putExtra("FromAc", Constant.MIAN);
                       startActivity(intent);
                       showMsg(t.getMessage());
                       finish();
                   }
                   break;
               default:
                   showMsg(t.getMessage());
           }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public <T extends BaseRequest>void  checkRequestBase(T t){
        loading();
    }
    public void loading(){
        if (dialog!=null&&dialog.isShowing())
            return;
        dialog=new ProgressDialog(this);
        dialog.setTitle("程序正在加载，请稍候...");
        dialog.show();
    }
    public void hideLoading(){
        if (dialog!=null)
            dialog.dismiss();
    }


    public User getUser(){
        return SharePreferenceUtil.getObject(this,User.class);
    }


    public boolean isLogin(User user){
        if (user==null||user.getAccessToken()==null||user.getAccessToken().length()<1)
            return false;
        return true;
    }

    /*******
     * 如果未登陆跳转登录，
     * @param user
     * @return
     */
    public boolean isLoginWithLogin(User user){
        if (user==null||user.getAccessToken()==null||user.getAccessToken().length()<1){
            startActivity(new Intent(this,LoginAc.class));
            return false;
        }
        return true;
    }

    /*********
     *
     * @param strings
     * @param views
     * @param msgView
     * @return
     */
    public boolean checkEmpty(int [] strings,TextView[] views,TextView msgView)
    {
        for (int i = 0; i < views.length; i++) {
            if (views[i].getText()==null||views[i].getText().length()<1)
            {
                if (msgView != null) {
                    msgView.setVisibility(View.VISIBLE);
                    msgView.setText(strings[i]);
                    return false;
                }
                Toast.makeText(this, strings[i],Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    public boolean checkEmpty(String[] strings,TextView[] views,TextView msgView)
    {

        for (int i = 0; i < views.length; i++) {
            if (views[i].getText()==null||views[i].getText().length()<1)
            {
                if (msgView != null) {
                    msgView.setVisibility(View.VISIBLE);
                    msgView.setText(strings[i]);
                    return false;
                }
                Toast.makeText(this, strings[i],Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    public void showMsg(String message) {
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFButton(int level) {

    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public <T> void netResponse(T t) {

    }


    public void hideSoftInputFromWindow(View view){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.text_banner_left:
                finish();
                break;
        }
    }
}
